package com.artmofang.service.Impl;

import com.artmofang.bo.StudentBO1;
import com.artmofang.mapper.StudentAndClassMapper;
import com.artmofang.mapper.StudentMapper;
import com.artmofang.pojo.Student;
import com.artmofang.service.StudentService;
import com.artmofang.vo.ErpStudentJoinCourseVO;
import com.artmofang.vo.StudentAndClassVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private StudentAndClassMapper studentAndClassMapper;

    @Override
    public List<Student> queryStudentAll() {
        return studentMapper.selectAll();
    }

    @Override
    public Student queryStudentById(Integer studentId) {
        Student student =  studentMapper.selectByPrimaryKey(studentId);
        return student;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int save(StudentBO1 studentBO1) {
        Student student = new Student();
        student.setAge(studentBO1.getAge());
        student.setName(studentBO1.getName());
        int result = studentMapper.insertUseGeneratedKeys(student);
        System.out.println("********************");
        System.out.println(student.getId());
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int update(Integer studentId, StudentBO1 studentBO1) {
        Student student = new Student();
        student.setId(studentId);

        student.setName(studentBO1.getName());
        student.setAge(studentBO1.getAge());

       return studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public int delete(Integer studentId) {
        return studentMapper.deleteByPrimaryKey(studentId);
    }

    @Override
    public List<StudentAndClassVO> queryStudentAndClassAll() {
        return studentAndClassMapper.queryStudentAndClassAll();
    }

    @Override
    public StudentAndClassVO queryStudentAndClassById(Map<String, Object> map) {
        return studentAndClassMapper.queryStudentAndClassById(map);
    }

}
